package com.jin.webvideo.externalInterfaces.video;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jin.webvideo.exception.UnavailableServiceException;
import com.jin.webvideo.externalInterfaces.PlatformSupport;
import com.jin.webvideo.externalInterfaces.entity.Filmography;
import com.jin.webvideo.externalInterfaces.entity.FilmographyInfo;
import com.jin.webvideo.externalInterfaces.entity.ParticipationMate;
import com.jin.webvideo.util.entity.EntityUtil;
import com.jin.webvideo.util.sys.ClientUtil;
import com.jin.webvideo.util.base.StringUtil;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Component
public class YZZYApi extends AbstractVideoAPI{
    private static final String URL = "https://api.1080zyku.com/inc/api_mac10.php?ac=detail&ids=%s";
    @Override
    String platformName() {
        return PlatformSupport.PLATFORM_YZZY;
    }

    @Override
    public Filmography baseInfo(String docid, String tvid) throws UnavailableServiceException {
        List<Filmography> list = detail(docid);
        if (list.size() > 0) {
            return list.get(0);
        }return null;
    }
    @Override
    public List<FilmographyInfo> videoInfoList(String docid, String tvid, Integer pageNo, Integer pageSize) throws UnavailableServiceException {
        Filmography filmography = baseInfo(docid, tvid);
        if (null != filmography) {
            return filmography.getVideoInfos();
        }
        return null;
    }
    public List<Filmography> detail(String docid) throws UnavailableServiceException {
        List<Filmography> list = new ArrayList<>();
        try {
            String data = ClientUtil.httpGet(String.format(URL, docid));
            JSONObject json = JSONObject.parseObject(data);
            JSONArray array = json.getJSONArray("list");
            for (int i = 0, l = array.size(); i < l; i++) {
                list.add(toFilmography(array.getJSONObject(i)));
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new UnavailableServiceException();
        }
        return list;
    }
    private Filmography toFilmography(JSONObject json) {
        Filmography filmography = new Filmography();
        filmography.setName(json.getString("vod_name"));
        filmography.setId(json.getString("vod_id"));
        filmography.setTag(json.getString("type_name"));
        filmography.setImg(json.getString("vod_pic"));
        filmography.setYear(json.getString("vod_year"));
        filmography.setDesc(json.getString("vod_content"));
        filmography.setPlatform(PlatformSupport.PLATFORM_YZZY);
        filmography.setPlatformName(PlatformSupport.PLATFORM_NAME_YZZY);
        ParticipationMate mate = new ParticipationMate();
        mate.setActor(EntityUtil.string2Part(json.getString("vod_actor")));
        mate.setDirector(EntityUtil.string2Part(json.getString("vod_director")));
        filmography.setMate(mate);
        filmography.setDisplayMode(FilmographyInfo.DISPLAY_MODE_INDEX_BLOCK);
        String videoInfos = json.getString("vod_play_url");
        List<FilmographyInfo> infos = new ArrayList<>();
        String[] list = StringUtil.split(videoInfos, "#");
        for (int i = 0; i < list.length; i++) {
            String[] kv = StringUtil.split(list[i], "$");
            FilmographyInfo filmographyInfo = new FilmographyInfo();
            filmographyInfo.setTitle(kv[0]);
            filmographyInfo.setUrl(kv[1]);
            filmographyInfo.setNumber(i+1);
            filmographyInfo.setPlatform(PlatformSupport.PLATFORM_YZZY);
            filmographyInfo.setDisplayMode(FilmographyInfo.DISPLAY_MODE_INDEX_BLOCK);
            filmographyInfo.setId(filmography.getId() + "_" + filmographyInfo.getNumber());
            infos.add(filmographyInfo);
        }
        filmography.setVideoInfos(infos);
        return filmography;
    }
}
